//
//  ST_SinglePeculiarityWaterfallController+Animate.swift
//  btvideo
//
//  Created by mac on 2020/4/27.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
import SwifterSwift
import SwiftyUserDefaults
import SDWebImage


func change_emoji_count_text(endCenter:CGPoint,jumpNumberLabel:UILabel,root_view:UIView){
    
    if jumpNumberLabel.superview == nil{
        
        root_view.addSubview(jumpNumberLabel)
        
        jumpNumberLabel.transform = CGAffineTransform.identity
        
        jumpNumberLabel.text = "0"
        
        //            self.jumpNumberLabel.frame = CGRect.init(x: 0, y: 0, width: 80, height: 40)
        
        jumpNumberLabel.snp.makeConstraints { (make) in
            
            make.center.equalTo(root_view)
//            make.top.equalToSuperview().offset(endCenter.y)
        }
        //
        //            self.jumpNumberLabel.center = CGPoint.init(x: endCenter.x, y: endCenter.y+50)
    }
    
    jumpNumberLabel.transform = CGAffineTransform.identity
    
    var number = 0
    
    if let n = (jumpNumberLabel.text ?? "0").int{
        
        number = n + 1
    }else{
        number = 1
    }
    
    
    
    let fontSize = 45+(number.cgFloat > 50 ? 50 :  number.cgFloat)
    
//    bomb_emoji_stroke_text_attributes.updateValue(UIFont.init(name: "Gotham-Black", size: fontSize)!, forKey: NSAttributedString.Key.font)
    
    bomb_emoji_stroke_text_attributes.updateValue(UIFont.init(name: "VAGRoundedBT-Regular", size: fontSize)!, forKey: NSAttributedString.Key.font)
    
    let customizedText = NSMutableAttributedString(string: number.string,
                                                   attributes: bomb_emoji_stroke_text_attributes)
    
    
    
    jumpNumberLabel.attributedText = customizedText
    
    let angle = Double.random(in: Double.pi / -10...Double.pi/10)
    
    let rotationTransform = CGAffineTransform.init(rotationAngle: angle.cgFloat)
    
    let tX = Double.random(in: -10...10).cgFloat
    
    let tY = Double.random(in: -10...10).cgFloat
    
    let translation = CGAffineTransform.init(translationX: tX, y: tY)
    
    //        var scaleX = (number.double / 50.0 * 2.0).cgFloat + 1.0
    //
    //        if scaleX > 2{
    //            scaleX = 2
    //        }
    //
    //        let sacleTransForm = CGAffineTransform.init(scaleX: scaleX, y: scaleX)
    
    let t = translation.concatenating(rotationTransform)
    
    //        t = t.concatenating(translation)
    
    jumpNumberLabel.transform = t
    
    var animateTransform = CGAffineTransform.init(scaleX: 1.5, y: 1.5)
    
    animateTransform = t.concatenating(animateTransform)
    
    UIView.animate(withDuration: 0.2, animations: {
        
        jumpNumberLabel.transform = animateTransform
        
    }) { (f) in
        
    }
    
}



fileprivate var  bomb_emoji_stroke_text_attributes:[NSAttributedString.Key : Any] = {
    
    let att = [
        NSAttributedString.Key.strokeColor.rawValue : Color.init(hexString: "#FA7B90")!,
        NSAttributedString.Key.foregroundColor : UIColor.init(hex: 0xFFE145)!,
        NSAttributedString.Key.strokeWidth : -8.0,
        ] as! [NSAttributedString.Key : Any]
    
    return att
}()
